package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.dao.SmsVersionDao;
import com.mz.entity.SmsBanner;
import com.mz.entity.SmsFeedback;
import com.mz.entity.SmsVersion;
import com.mz.entity.vo.CurVersionVo;
import com.mz.entity.vo.VersionInfoVo;
import com.mz.service.SmsVersionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SmsVersion)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:34
 */
@Service
public class SmsVersionServiceImpl  implements SmsVersionService {

    @Resource
    private SmsVersionDao smsVersionDao;

    @Override
    public IPage getVersionPagination(PaginationVo paginationVo) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<SmsVersion> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(SmsVersion::getCreateTime);

        smsVersionDao.selectPage(page, lqs);
        return page;
    }

    @Override
    public void addVersionInfo(VersionInfoVo versionInfo) throws Exception {
        if( versionInfo.getId() != null ){
            throw GlobalException.gen(10033, "修改版本信息接口对接错误");
        }

        //判断是否存在了同样的版本
        LambdaQueryWrapper<SmsVersion> lqs = new LambdaQueryWrapper<>();
        lqs.eq(SmsVersion::getPlatformType, versionInfo.getPlatformType());
        lqs.eq(SmsVersion::getUpgradeVersion, versionInfo.getUpgradeVersion());
        List<SmsVersion> smsVersions = smsVersionDao.selectList(lqs);
        if( smsVersions != null && smsVersions.size() > 0 ){
            throw GlobalException.gen(10034, "版本已存在，请设置更高版本");
        }

        SmsVersion smsVersion = new SmsVersion();
        BeanUtils.copyProperties(versionInfo, smsVersion);
        smsVersionDao.insert(smsVersion);
    }

    @Override
    @Transactional
    public void updateVersionInfo( CurVersionVo curVersionVo) throws Exception {

        LambdaQueryWrapper<SmsVersion> lqs = new LambdaQueryWrapper<>();
        lqs.eq(SmsVersion::getIsCur, 1);
        lqs.eq(SmsVersion::getPlatformType, curVersionVo.getPlatformType());
        SmsVersion oldVersion = smsVersionDao.selectOne(lqs);
        if( oldVersion != null ){
            oldVersion.setIsCur(0);
            smsVersionDao.updateById(oldVersion);
        }

        //更新版本信息
        SmsVersion smsVersion = new SmsVersion();
        smsVersion.setId(curVersionVo.getId())
                .setPlatformType(curVersionVo.getPlatformType())
                .setIsCur(1);
        smsVersionDao.updateById(smsVersion);
    }

    @Override
    public void removeVersionInfo(List<Long> idList) throws Exception {
        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10014, "版本id不存在");
        }
        smsVersionDao.deleteBatchIds(idList);
        return;
    }
}

